package com.nageoffer.subject.domain.service.impl;

import com.nageoffer.subject.common.enums.IsDeletedFlagEnums;
import com.nageoffer.subject.domain.convert.SubjectCategoryBOConverter;
import com.nageoffer.subject.domain.entity.SubjectCategoryBO;
import com.nageoffer.subject.domain.service.SubjectCategoryDomainService;
import com.nageoffer.subject.infra.basic.entity.SubjectCategory;
import com.nageoffer.subject.infra.basic.service.SubjectCategoryService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@RequiredArgsConstructor
public class SubjectCategoryDomainServiceImpl implements SubjectCategoryDomainService {
    private final SubjectCategoryService subjectCategoryService;

    @Override
    public void add(SubjectCategoryBO subjectCategoryBO) {
        SubjectCategory subjectCategory = SubjectCategoryBOConverter
                .INSTANCE.convertBoToCategory(subjectCategoryBO);
        subjectCategory.setIsDeleted(IsDeletedFlagEnums.UN_DELETED.getCode());
        subjectCategory.setCreatedTime(new Date());
        subjectCategory.setUpdateTime(new Date());
        subjectCategoryService.insert(subjectCategory);
    }

    @Override
    public List<SubjectCategoryBO> queryCategory(SubjectCategoryBO subjectCategoryBO) {
        SubjectCategory subjectCategory = SubjectCategoryBOConverter.INSTANCE
                .convertBoToCategory(subjectCategoryBO);
        subjectCategory.setIsDeleted(IsDeletedFlagEnums.UN_DELETED.getCode());
        List<SubjectCategory> subjectCategoryList = subjectCategoryService.queryCategory(subjectCategory);
        List<SubjectCategoryBO> subjectCategoryBOList = SubjectCategoryBOConverter.INSTANCE
                .convertCategoryListToCategoryBoList(subjectCategoryList);
        return subjectCategoryBOList;
    }

    @Override
    public Boolean updateCategory(SubjectCategoryBO subjectCategoryBO) {
        SubjectCategory subjectCategory = SubjectCategoryBOConverter.INSTANCE
                .convertBoToCategory(subjectCategoryBO);
        subjectCategory.setUpdateTime(new Date());
        Integer result = subjectCategoryService.update(subjectCategory);
        return result > 0;
    }

    @Override
    public Boolean deleteCategory(SubjectCategoryBO subjectCategoryBO) {
        SubjectCategory subjectCategory = SubjectCategoryBOConverter.INSTANCE
                .convertBoToCategory(subjectCategoryBO);
        subjectCategory.setIsDeleted(IsDeletedFlagEnums.DELETED.getCode());
        subjectCategory.setUpdateTime(new Date());
        Integer result = subjectCategoryService.update(subjectCategory);
        return result > 0;
    }
}
